package com.itsu.job.rpc.request;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import com.itsu.job.context.JobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @author Jerry.Su
 * @Date 2022/1/28 14:36
 */
@Slf4j
public class StatusJobRequestHandler extends JobRpcRequestHandler {

    final JobHelper jobHelper;

    public StatusJobRequestHandler(JobHelper jobHelper) {
        this.jobHelper = jobHelper;
    }

    @Override
    public Map<String, Object> apply(String uri, String requestData) {
        MapBuilder<String, Object> builder = MapUtil.builder();
        try {
            String jobId = getJobId(requestData);
            Assert.notNull(jobId, "jobId can not be null");
            int status = jobHelper.status(jobId);
            builder.put("code", 0);
            builder.put("data", status);
        } catch (Exception e) {
            log.error("get job status error ", e);
            builder.put("code", -1);
            builder.put("msg", e.getMessage());
        }
        return builder.build();
    }
}
